import { Component, OnInit, EventEmitter, Input, Output, ElementRef } from '@angular/core';

import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { Service } from '../../../server/server';
import { Datatype } from '../institution-table/institution-table.component';
declare var $: any;

@Component({
  selector: 'app-institution-update',
  templateUrl: './institution-update.component.html',
})
export class InstitutionUpdateComponent implements OnInit {

  constructor(private server: Service, private cudTableService: CudTableService) { }

  ngOnInit() {
    this.createUserId = this.server.getuser();
    //console.log(this.getTabVal)
    //接收tree数据
    this.institutiontreeidnameeventEmit = this.server.institutiontreeidnameeventEmit.subscribe((value: any) => {
      this.superiorUpdate = value.institutionName;
      this.superiorUpdateId = value.institutionId;
    })
  }

  //取消订阅
  public institutiontreeidnameeventEmit: any;
  ngOnDestroy() {
    if (this.institutiontreeidnameeventEmit) {
      this.institutiontreeidnameeventEmit.unsubscribe();
    }
  }

  ngDoCheck() {
    //this.getTabVal.validDate = new Date(this.getTabVal.validDate);
  }

  public createUserId: any;

  @Input() getTabVal: Datatype;

  @Input() superiorUpdate: any;

  @Input() superiorUpdateId: any;

  @Output()
  updateOutput: EventEmitter<any[]> = new EventEmitter();

  submitEdit() {
    //console.log(this.getTabVal.validDate)
    let url = this.server.baseUrl + 'admin/institutions/modify';
    let data: Datatype = {
      createUserId: this.createUserId,
      lastUpdatedDate: "",
      lastUpdatedUserId: "",
      createDate: "",
      parentId: this.getTabVal.parentId,
      institutionId: this.getTabVal.institutionId,
      institutionOrder: "",
      institutionName: this.getTabVal.institutionName,
      institutionDisplayName: this.getTabVal.institutionDisplayName,
      nature: this.getTabVal.nature,
      contactName: this.getTabVal.contactName,
      industry: this.getTabVal.industry,
      institutionType: this.getTabVal.institutionType,
      jgServerType: this.getTabVal.jgServerType,
      enabled: Number(this.getTabVal.enabled),
      validDate: this.server.formatDate(this.getTabVal.validDate),
      phone: this.getTabVal.phone,
      postcode: this.getTabVal.postcode,
      province: this.getTabVal.province,
      city: this.getTabVal.city,
      address: this.getTabVal.address,
      description: this.getTabVal.description,
    }
    //console.log(data)
    this.cudTableService.postRequest(url, data).then((res: any) => {
      if (res.result_code === '0000') {
        $('#update').modal('hide');
        //prompt
        this.server.prompt = { prompt: true, success: true, fail: false, promptStatement: res.message };
        this.server.promptclose;
        this.updateOutput.emit(res.result_code);
        //update
      } else {
        this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: res.message };
        this.server.promptclose;
      }
    }).catch()

  }

}
